<template>
  <div class="app-container">
    <input type="text" autocomplete="off" v-model="stationCode" placeholder="展点id">
    <input type="text" autocomplete="off" v-model="stationName" placeholder="展点名称">
    <button type="button" @click="fetchStationList">
      <span>查询</span>
    </button>
    <button type="button" @click="reseachStation">
      <span>重置</span>
    </button>
    <br>
    <!-- 新增测试展点 -->
    <el-button type="text" @click="dialogVisible = true">新增测试展点</el-button>
    <el-dialog title="新增测试展点" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">

      <el-form ref="addStationForm" :model="form" label-width="150px" :rules="stationRules">
        <el-form-item label="展点名称" prop="no">
          <el-input v-model.trim="form.name" ></el-input>
        </el-form-item>
        <el-form-item label="展点code" prop="no">
          <el-input v-model.trim="form.stationCode"></el-input>
        </el-form-item>
        <el-form-item label="展点MongoDB" prop="no">
          <el-input v-model.trim="form.mongocode"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="createStation('addStationForm')">立即创建</el-button>
          <el-button @click = "concelAddStation()">重置</el-button>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>


    <!-- 展示展点列表 -->
    <el-table v-loading="listLoading" :data="stationList" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="id" width="100">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="展点短ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.stationCode }}
        </template>
      </el-table-column>
      <el-table-column label="展点名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.stationName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="展点长ID" align="center">
        <template slot-scope="scope">
          {{ scope.row.stationMongoCode }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button type="text"
            @click="deleteStation(scope.row.id)">删除展点</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getStationList } from '@/api/station/station'
import { addStation } from '@/api/station/station'
import { changeStationStatus } from '@/api/station/station'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      stationCode: "",
      stationName: "",
      dialogVisible: false,
      form: {
        name: '',
        stationCode: '',
        mongocode: ''
      },
      stationList: null,
      stationRules: {
        no: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.fetchStationList()
  },
  methods: {
    fetchStationList() {
      this.listLoading = true
      getStationList({
        "stationCode": this.stationCode,
        "stationName": this.stationName
      }).then(response => {
        console.log(response)
        this.stationList = response.data
        this.listLoading = false
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    createStation() {
      addStation(
        {
          "stationCode": this.form.stationCode,
          "stationName": this.form.name,
          "stationMongoCode": this.form.mongocode
        }
      ).then(response => {
        this.reseachStation()
        this.dialogVisible = false
        this.$message.success(`${response.message}`)
      })
    },
    reseachStation() {
      this.stationCode = ""
      this.stationName = ""
      this.fetchStationList()
    },
    hidDialog() {
      this.dialogVisible = false
    },
    deleteStation(stationId) {
      alert("确定删除展点？")
      changeStationStatus({
        "id": stationId
      }).then(
        response => {
          this.$message.success(`${response.message}`)
          this.fetchStationList()
        }
      )
    },
    concelAddStation(){
      this.form.name = "",
      this.form.stationCode = "",
      this.form.mongocode = ""
    }

  }
}
</script>

<style></style>